package jp.seraph.jsade.sexpression;

/**
 * S式ノードをString形式に変換するビジタ
 *
 *
 */
public class StringVisitor implements ExpressionVisitor {
    public StringVisitor(){
        mBuilder = new StringBuilder();
    }

    private StringBuilder mBuilder;

    
    public void visit(Leaf aExpression) {
        mBuilder.append(aExpression.getValue());
    }

    
    public void visit(Node aExpression) {
        int tCount;
        mBuilder.append("(");
        if((tCount = aExpression.childCount()) > 0){
            aExpression.getChild(0).accept(this);
            for(int tIndex=1; tIndex < tCount; tIndex++){
                mBuilder.append(" ");
                aExpression.getChild(tIndex).accept(this);
            }
        }
        mBuilder.append(")");
    }

    public String getString(){
        return mBuilder.toString();
    }

    
    public String toString(){
        return getString();
    }
}
